package com.game77.guessTheWords2.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import android.util.Base64;
import android.util.Log;

import com.game77.guessTheWords2.MainActivity;
import com.game77.guessTheWords2.entity.CharacterLocationInfo;
import com.game77.guessTheWords2.entity.MapInfo;
import com.game77.guessTheWords2.entity.WordInfo;

/**
 * 地图加载器
 * 
 * @author davelin
 * 
 */
public class Ccc {

	public static int a(String stageName) {
		try {
			String fileName = "stage" + stageName + ".map";
			InputStream is = MainActivity.instance.getAssets().open(fileName);
			InputStreamReader sr = new InputStreamReader(is, "UTF-8");
			BufferedReader br = new BufferedReader(sr, 1500);

			// 读取wordCount
			String c = br.readLine().trim();
			String count = new String(Base64.decode(c, Base64.DEFAULT), "UTF-8");

			return Integer.valueOf(count);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		} 
	}

	public static MapInfo b(String stageName) {
		MapInfo mapInfo = new MapInfo();
		String fileName = "stage" + stageName + ".map";
		try {

			InputStream is = MainActivity.instance.getAssets().open(fileName);
			InputStreamReader sr = new InputStreamReader(is, "UTF-8");
			BufferedReader br = new BufferedReader(sr, 1500);

			// 读取wordCount
			String c = br.readLine().trim();
			String count = new String(Base64.decode(c, Base64.DEFAULT), "UTF-8");
			int wordCount = Integer.valueOf(count);
			mapInfo.setWordsCount(wordCount);

			// 读取所有词条
			List<WordInfo> words = new ArrayList<WordInfo>();
			for (int i = 0; i < wordCount; i++) {
				String wordComponentTemp = br.readLine();
				String wordComponent = new String(Base64.decode(
						wordComponentTemp, Base64.DEFAULT), "UTF-8");
				String[] compoents = wordComponent.split("--");
				WordInfo info = new WordInfo();
				info.setPinyin(compoents[0]);
				info.setContent(compoents[1]);
				info.setTip(compoents[2]);
				words.add(info);
			}
			mapInfo.setWordList(words);

			// 读取字符位置
			List<CharacterLocationInfo> locations = new ArrayList<CharacterLocationInfo>();
			String location64 = br.readLine();
			int lineIndex = 0;

			while (location64 != null && !location64.equals("")) {

				CharacterLocationInfo locationInfo = new CharacterLocationInfo();

				String location = new String(Base64.decode(location64,
						Base64.DEFAULT), "UTF-8");

				String[] locationComponent = location.split(",");
				locationInfo.setRow(Integer.parseInt(locationComponent[0]));
				locationInfo.setColumn(Integer.parseInt(locationComponent[1]));

				if (locationComponent[2] != null
						&& !locationComponent[2].equals("N")) {
					locationInfo.setOrientationWordIndex(Integer
							.parseInt(locationComponent[2]));
					locationInfo.setOrientationCharIndex(Integer
							.parseInt(locationComponent[3]));
				}
				if (locationComponent[4] != null
						&& !locationComponent[4].equals("N")) {
					locationInfo.setVerticalWordIndex(Integer
							.parseInt(locationComponent[4]));
					locationInfo.setVerticalCharIndex(Integer
							.parseInt(locationComponent[5]));
				}
				locations.add(locationInfo);

				location64 = br.readLine();
				lineIndex++;
			}

			mapInfo.setCharLocationList(locations);

			// 关闭Stream
			br.close();
			sr.close();
			is.close();
		} catch (Exception ex) {
			Log.e("loadmap", ex.getMessage(), ex);
		}

		return mapInfo;
	}
}
